package com.iknow.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.data.QingBo;
import com.iknow.data.QingboType;
import com.iknow.file.CacheSystem;
import com.iknow.image.ImageManager;
import com.iknow.image.ProfileImageCacheManager;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import com.mobclick.android.MobclickAgent;
import java.io.File;
import java.util.Date;
import org.jivesoftware.smack.util.StringUtils;

public class WriteBlogActivity extends Activity
{
  private static final int REQUEST_IMAGE_CAPTURE = 2;
  private static final int REQUEST_PHOTO_LIBRARY = 3;
  private static final Intent SERVICE_INTENT = new Intent();
  private View.OnClickListener BackButtonClickListener;
  private View.OnClickListener ContentImageClickListener;
  private View.OnClickListener SaveButtonClickListener;
  private final String TAG = "WriteBlogActivity";
  private Button mBackButton;
  private boolean mBinded;
  private EditText mContentEditText;
  private ImageView mContentImage;
  private String mCurrentSelectPhoto;
  private File mImageFile;
  private Uri mImageUri;
  private boolean mPickImaged;
  private ProgressDialog mProgressDialg;
  private Button mSaveButton;
  private Bitmap mSelectBimap;
  private ServiceConnection mSvrConn;
  private TextView mTagTextView;
  private WriteTask mTask;
  private TaskListener mTaskListener;
  private EditText mTitleEditText;
  private QingboType mType;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public WriteBlogActivity()
  {
    this.mSvrConn = new XmppServiceConnection(this, null);
    this.mCurrentSelectPhoto = null;
    this.mPickImaged = false;
    this.BackButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    };
    this.SaveButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (!(IKnow.IsNetAviable()))
          Toast.makeText(this.this$0, this.this$0.getString(2131361878), 0).show();
        while (true)
        {
          String str;
          while (true)
          {
            while (true)
            {
              return;
              str = WriteBlogActivity.access$0(this.this$0).getText().toString();
              if (WriteBlogActivity.access$1(this.this$0) != QingboType.image)
                break;
              if (!(StringUtil.isEmpty(WriteBlogActivity.access$2(this.this$0))))
                break label148;
              Toast.makeText(this.this$0, this.this$0.getString(2131361943), 0).show();
            }
            if ((WriteBlogActivity.access$1(this.this$0) != QingboType.text) || (!(StringUtil.isEmpty(str))) || (!(StringUtil.isEmpty(WriteBlogActivity.access$2(this.this$0)))))
              break;
            Toast.makeText(this.this$0, this.this$0.getString(2131361944), 0).show();
          }
          label148: QingBo localQingBo = WriteBlogActivity.access$3(this.this$0);
          WriteBlogActivity.access$4(this.this$0, localQingBo, str, WriteBlogActivity.access$2(this.this$0));
        }
      }
    };
    this.ContentImageClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (WriteBlogActivity.access$5(this.this$0));
        while (true)
        {
          return;
          WriteBlogActivity.access$6(this.this$0);
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "WriteTask";
      }

      public void onCancelled()
      {
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (WriteBlogActivity.access$8(this.this$0) != null)
          WriteBlogActivity.access$8(this.this$0).dismiss();
        if (paramTaskResult == TaskResult.OK)
        {
          Toast.makeText(this.this$0, "发布成功", 0).show();
          Intent localIntent = new Intent(this.this$0, MyQingboActivity.class);
          this.this$0.startActivity(localIntent);
          this.this$0.finish();
        }
        while (true)
        {
          return;
          Toast.makeText(this.this$0, WriteBlogActivity.access$9(this.this$0).getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
        WriteBlogActivity.access$7(this.this$0, new ProgressDialog(this.this$0));
        WriteBlogActivity.access$8(this.this$0).setMessage("发布中，请稍等...");
        WriteBlogActivity.access$8(this.this$0).show();
      }
    };
  }

  private void doWrite(QingBo paramQingBo, String paramString1, String paramString2)
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      MobclickAgent.onEvent(this, "share_click");
      this.mTask = new WriteTask(this, null);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("qingbo", paramQingBo);
      localTaskParams.put("data", paramString1);
      localTaskParams.put("file", paramString2);
      this.mTask.setListener(this.mTaskListener);
      WriteTask localWriteTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localWriteTask.execute(arrayOfTaskParams);
    }
  }

  private void initView()
  {
    setContentView(2130903138);
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mBackButton.setOnClickListener(this.BackButtonClickListener);
    this.mSaveButton = ((Button)findViewById(2131296344));
    this.mSaveButton.setText("发布");
    this.mSaveButton.setOnClickListener(this.SaveButtonClickListener);
    this.mContentImage = ((ImageView)findViewById(2131296534));
    this.mContentImage.setOnClickListener(this.ContentImageClickListener);
    this.mContentEditText = ((EditText)findViewById(2131296531));
    this.mTitleEditText = ((EditText)findViewById(2131296596));
    this.mTagTextView = ((TextView)findViewById(2131296599));
  }

  private QingBo newTextQingBo()
  {
    String str1 = this.mTagTextView.getText().toString();
    String str2 = this.mContentEditText.getText().toString();
    String str3 = this.mTitleEditText.getText().toString();
    if (StringUtil.isEmpty(str3))
    {
      if (this.mType != QingboType.image)
        break label136;
      str3 = "这用英语怎么说";
    }
    while (true)
    {
      QingBo localQingBo = new QingBo("", str3, str2, new Date(System.currentTimeMillis()), null, "我", StringUtils.parseName(IKnow.mSystemConfig.getString("account_username")));
      localQingBo.setQingboType(this.mType);
      if (!(StringUtil.isEmpty(this.mCurrentSelectPhoto)))
        localQingBo.setQingboType(QingboType.image);
      localQingBo.setTag(str1);
      return localQingBo;
      label136: str3 = "爱英语,爱分享!";
    }
  }

  private void onImageSelected(Uri paramUri)
  {
    if (paramUri == null)
      return;
    if (paramUri.getScheme().equals("content"))
      this.mImageUri = paramUri;
    while (true)
    {
      while (true)
      {
        ImageManager localImageManager = IKnow.mProfileImageCacheManager.getImageManager();
        String str = IKnow.mCacheSystem.getThumbmailFileName(paramUri.getPath());
        this.mSelectBimap = localImageManager.createThumbnailBitmap(this.mImageUri, 800);
        this.mCurrentSelectPhoto = localImageManager.bitmapToFile(this.mSelectBimap, str).getAbsolutePath();
        this.mContentImage.setImageBitmap(localImageManager.createThumbnailBitmap(this.mImageUri, 400));
        this.mContentImage.setVisibility(0);
        this.mPickImaged = true;
      }
      this.mImageUri = Uri.fromFile(new File(paramUri.getPath()));
    }
  }

  private void onImageSelected(String paramString)
  {
    ImageManager localImageManager = IKnow.mProfileImageCacheManager.getImageManager();
    String str = IKnow.mCacheSystem.getThumbmailFileName(paramString);
    localImageManager.bitmapToFile(localImageManager.createThumbnailBitmap(Uri.parse("file://" + paramString), 600), str);
    this.mCurrentSelectPhoto = str;
    this.mContentImage.setImageURI(Uri.parse("file://" + paramString));
    this.mContentImage.setVisibility(0);
  }

  private void openImageCaptureMenu()
  {
    try
    {
      this.mImageFile = new File(IKnow.mCacheSystem.createImagefileName());
      this.mImageUri = Uri.fromFile(this.mImageFile);
      Intent localIntent = new Intent("android.media.action.IMAGE_CAPTURE");
      localIntent.putExtra("output", this.mImageUri);
      startActivityForResult(localIntent, 2);
      return;
    }
    catch (Exception localException)
    {
      Loger.e("WriteBlogActivity", localException.getMessage());
    }
  }

  private void openPhotoLibraryMenu()
  {
    Intent localIntent = new Intent("android.intent.action.GET_CONTENT");
    localIntent.setType("image/*");
    startActivityForResult(localIntent, 3);
  }

  private void showImageSelecte()
  {
    CharSequence[] arrayOfCharSequence = new CharSequence[2];
    arrayOfCharSequence[0] = getString(2131361973);
    arrayOfCharSequence[1] = getString(2131361974);
    AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
    localBuilder.setTitle(getString(2131361975));
    localBuilder.setItems(arrayOfCharSequence, new DialogInterface.OnClickListener(this)
    {
      public void onClick(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case 0:
        case 1:
        }
        while (true)
        {
          while (true)
          {
            return;
            WriteBlogActivity.access$12(this.this$0);
          }
          WriteBlogActivity.access$13(this.this$0);
        }
      }
    });
    localBuilder.create().show();
  }

  protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent)
  {
    super.onActivityResult(paramInt1, paramInt2, paramIntent);
    if ((paramInt1 == 2) && (paramInt2 == -1))
      onImageSelected(this.mImageUri);
    while (true)
    {
      do
        return;
      while ((paramInt1 != 3) || (paramInt2 != -1));
      this.mImageUri = paramIntent.getData();
      onImageSelected(this.mImageUri);
    }
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    this.mType = QingboType.formString(getIntent().getStringExtra("type"));
    initView();
    if (this.mType == QingboType.image)
    {
      showImageSelecte();
      this.mTagTextView.setText("英语问答");
    }
    while (true)
    {
      return;
      this.mTagTextView.setText("英语分享");
    }
  }

  protected void onDestroy()
  {
    if (this.mTask != null)
      this.mTask.cancel(true);
    if ((this.mSelectBimap != null) && (!(this.mSelectBimap.isRecycled())))
    {
      this.mSelectBimap.recycle();
      this.mSelectBimap = null;
    }
    super.onDestroy();
  }

  protected void onPause()
  {
    if (this.mBinded)
    {
      unbindService(this.mSvrConn);
      this.mBinded = false;
    }
    MobclickAgent.onPause(this);
    super.onPause();
  }

  protected void onResume()
  {
    if (!(this.mBinded))
      this.mBinded = bindService(SERVICE_INTENT, this.mSvrConn, 1);
    MobclickAgent.onResume(this);
    MobclickAgent.setSessionContinueMillis(600000L);
    super.onResume();
  }

  private class WriteTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        if ((WriteBlogActivity.access$10(this.this$0) == null) || (WriteBlogActivity.access$10(this.this$0).createConnection() == null) || (!(WriteBlogActivity.access$10(this.this$0).createConnection().authenticatContentUser())))
        {
          this.mErrorMessage = "网络超时，稍候再试";
          localTaskResult = TaskResult.FAILED;
        }
        else
        {
          QingBo localQingBo = (QingBo)localTaskParams.get("qingbo");
          String str1 = localTaskParams.getString("data");
          String str2 = localTaskParams.getString("file");
          if (StringUtil.isEmpty(str2))
          {
            IKnow.mApi.writeTextQingBo(localQingBo, StringUtil.formatHtml(str1));
            localTaskResult = TaskResult.OK;
          }
          else
          {
            IKnow.mApi.writeQingBo(localQingBo, StringUtil.formatHtml(str1), str2);
          }
        }
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        this.mErrorMessage = "网络超时，稍候再试";
        localTaskResult = TaskResult.FAILED;
        return localTaskResult;
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      WriteBlogActivity.access$11(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
    }

    public void onServiceDisconnected()
    {
      WriteBlogActivity.access$11(this.this$0, null);
    }
  }
}